﻿using System;
using System.Collections.Generic;
using System.Text;

using System.Runtime.InteropServices;

using SuperMapLib;

namespace JR_RouteAlarm
{
    class CrossArea
    {
        private soDatasets objDSs = null;
        private AxSuperMapLib.AxSuperMap objMap = null;
        private string strLayerName = String.Empty;

        public CrossArea(AxSuperMapLib.AxSuperMap objMap, Utility utl)
        {
            utl.InitializeMap(objMap);
            this.objDSs = utl.objSource.Datasets;
            this.objMap = objMap;
        }

        public void BuildLayerForMap()
        {            
            AddBufferLayerToMap();
            strLayerName = objDSs[objDSs.Count].Name + "@" + Utility.datasourceAlias;
            SetTheme(objMap.Layers[strLayerName]);

            objMap.SaveMap();
        }

        private void AddBufferLayerToMap()
        {
            soLayers objLayers = null;
            soLayer objLayer200m = null;
            soLayer objLayerSrc = null;

            objLayers = objMap.Layers;
            objLayer200m = objMap.Layers.AddDataset(objDSs[objDSs.Count], false);
            if (objLayer200m != null)
            {
                objLayerSrc = objLayers["joukoujouR@areamap"];
                if (!objLayers.MoveTo(objLayers.Count, objLayers.FindLayer(objLayerSrc)))
                {
                    throw new Exception("Layer move failed!");
                }

                //objMap.Refresh();

                Marshal.ReleaseComObject(objLayerSrc);
                objLayerSrc = null;
                Marshal.ReleaseComObject(objLayer200m);
                objLayer200m = null;
            }

            Marshal.ReleaseComObject(objLayers);
            objLayers = null;
            //Marshal.ReleaseComObject(objMap);
            //objMap = null;
        }

        private void SetTheme(soLayer objLayer)
        {
            SetColor(objLayer);
            SetLabel(objLayer);

            Marshal.ReleaseComObject(objLayer);
            objLayer = null;
        }

        private void SetColor(soLayer objLayer)
        {
            soThemeUnique objThemeUnique = null;

            objThemeUnique = objLayer.ThemeUnique;
            if (objThemeUnique.Valid)
            {
                objLayer.DeleteThematicMap(objThemeUnique.Caption);
            }

            soColors objColors = null;
            soStyle objStyle = null;

            objThemeUnique.Field = Utility.CROSS_AREA_FIELD;
            objThemeUnique.Filter = 
                Route.FLD_FEATURE_CD + ", " + Route.FLD_DAIBUN_CD + ", " + Route.FLD_TSUUSHO_CD;
            objThemeUnique.Caption = Utility.THEME_COLOR_CAPTION;
            objThemeUnique.MakeDefault();

            objColors = new soColors();
            objColors.MakeRandomColorset(objThemeUnique.ValueCount);
            objStyle = objThemeUnique.DefaultStyle;
            objStyle.BrushStyle = 100;
            for (int i = 0; i < objThemeUnique.ValueCount; i++)
            { 
                objStyle.BrushColor = objColors[i];
                objThemeUnique.set_Style(i, objStyle);
                objThemeUnique.set_ItemCaption(i, (i+1).ToString());
            }

            Marshal.ReleaseComObject(objStyle);
            objStyle = null;
            Marshal.ReleaseComObject(objColors);
            objColors = null;
            Marshal.ReleaseComObject(objThemeUnique);
            objThemeUnique = null;
        }

        private void SetLabel(soLayer objLayer)
        {
            soThemeLabel objThemeLabel = null;

            objThemeLabel = objLayer.ThemeLabel;
            if (objThemeLabel.Valid)
            {
                objLayer.DeleteThematicMap(objThemeLabel.Caption);
            }

            soTextStyle objTextStyle = null;

            objThemeLabel.Field = Route.FLD_LABEL_TEXT;
            objThemeLabel.Filter =
                Route.FLD_FEATURE_CD + ", " + Route.FLD_DAIBUN_CD + ", " + Route.FLD_TSUUSHO_CD;
            objThemeLabel.Caption = Utility.THEME_LABLE_CAPTION;
            objThemeLabel.BackShape = seTextBackShape.sctBackRect;
            //objThemeLabel.AutoAvoidOverlapped = true;
            objThemeLabel.LineDirection = seLabelLineDirection.sclDirectionLeftBottomToRightTop;
            objThemeLabel.ShowLeaderLines = true;
            objThemeLabel.EnableFlow = true;
            objThemeLabel.TextControlMode = seTextControlMode.scmTextControlWrapText;
            objThemeLabel.SingleLineCharCount = Utility.MAX_LINE_CHAR_COUNT;
            objThemeLabel.OnTop = true;
            objTextStyle = new soTextStyle();
            objTextStyle.FontHeight = Utility.LABEL_FONT_HIGHT;
            objTextStyle.FontWidth = Utility.LABEL_FONT_WIDTH;
            objThemeLabel.TextStyle = objTextStyle;

            Marshal.ReleaseComObject(objTextStyle);
            objTextStyle = null;
            Marshal.ReleaseComObject(objThemeLabel);
            objThemeLabel = null;
        }
    }
}
